package com.wl.api.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentType;

import com.aliyun.api.gateway.demo.constant.HttpHeader_ali;
import com.aliyun.api.gateway.demo.constant.HttpSchema;
import com.tool.http.httpclient.HttpClientUtil;
import com.tool.util.http.common.HttpConfig;
import com.tool.util.http.common.HttpHeader;

public class Test {
	
	public static String post() throws Exception{
	      String a= "LOGISITCS:roadage2017515";
	        String author = "Basic " + BASE64Util.base64Encode(a.getBytes());
	        HttpConfig config = HttpConfig.custom();
	        HttpHeader hheader = HttpHeader.custom();
	        Map<String, Object> contentMap = new HashMap<>();
	        hheader.other(HttpHeader_ali.HTTP_HEADER_ACCEPT, "application/json");
	        hheader.other(HttpHeader_ali.HTTP_HEADER_CONTENT_TYPE, "application/json; charset=UTF-8");
	        hheader.other("Authorization", author);
	        HttpResponse response = HttpClientUtil.postWithHttpResponse(config.url(HttpSchema.HTTPS + "api.dpd.co.uk/user?action=login").headers(hheader.build()).paramap(contentMap));
	        System.out.println(IOUtils.toString(response.getEntity().getContent()));
			return author;
	}
	
	 public static String post(String urlPath, String param) throws Exception {  
	        // 设置参数  
	       // String param = "{\"userid\":" + userid + "}";
		    
	        // 建立连接  
	        URL url = new URL(urlPath);  
	        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
	        httpConn.setDoOutput(true); // 需要输出  
	        httpConn.setDoInput(true); // 需要输入  
	        httpConn.setUseCaches(false); // 不允许缓存  
	        httpConn.setRequestMethod("POST");  
	        // 设置请求属性  
	        httpConn.setRequestProperty("Content-Type",  
	                "application/x-www-form-urlencoded");  
	        // 维持长连接  
	        httpConn.setRequestProperty("Connection", "Keep-Alive");  
	        httpConn.setRequestProperty("Charset", "UTF-8");  
	        // 连接,也可以不用明文connect，使用下面的httpConn.getOutputStream()会自动connect  
	        httpConn.connect();  
	        // 建立输入流，向指向的URL传入参数  
	        httpConn.getOutputStream().write(param.getBytes());// 输入参数  
	  
	        // 获得响应状态  
	        int resultCode = httpConn.getResponseCode();  
	        if (HttpURLConnection.HTTP_OK == resultCode) {  
	            StringBuffer sb = new StringBuffer();  
	            String readLine = new String();  
	            BufferedReader responseReader = new BufferedReader(  
	                    new InputStreamReader(httpConn.getInputStream(), "UTF-8"));  
	            while ((readLine = responseReader.readLine()) != null) {  
	                sb.append(readLine).append("\n");  
	            }  
	            responseReader.close();  
	            System.out.println(sb.toString());  
	            return sb.toString();  
	        }  
	        return "error";  
	    }  

	public static void testGetuserDetail() {
		String str = "{\"userid\":2017050501}";
		System.out.println(URLEncoder.encode(str));
		System.out.println(URLDecoder.decode("%7B%22userid%22%3AZWX%7D"));
		 String access_token = "crZ6--vP5x7X5xnWrHipJW1XsjVFgScL9KEJxmZAIUDwT__7mLtPdpSChpnmacy3";  
		 String ticket = "ri6HzAct0DMdznlwv4VJ4ihSIt0EXLWIu3nzc7CQfAsZqnyEl5PmSbN26CJGCocXVPeTpy1xk9jz-jSs5SppADHVME1uQKhT52HNc4Ea_NM";
		 String  param = "{\"user_ticket\":\"ri6HzAct0DMdznlwv4VJ4ihSIt0EXLWIu3nzc7CQfAsZqnyEl5PmSbN26CJGCocXVPeTpy1xk9jz-jSs5SppADHVME1uQKhT52HNc4Ea_NM\"}";
	    // post("https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token="+access_token, param);
	    /* String userId = "{\"userid\":\"ZWX\"}";  
	     post("https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token="+access_token,  
	    		 userId);  
		
	     String body = "{\"userid\":\"ZWX\"}";
		String openIdObj = HttpClientUtil.post(HttpConfig.custom().url("http://qydev.weixin.qq.com/cgi-bin/apiagent?tid=73&access_token="+access_token+"&body=" + URLEncoder.encode(body) + "&f=json")); 
	     */
			//String openIdObj = HttpClientUtil.post(HttpConfig.custom().url("http://qydev.weixin.qq.com/cgi-bin/apiagent?tid=73&access_token=aKVkxvEh8__46HxbOviCKxQEBbh_mZPC4L7X9Mie2q8dfu4XR8OePO9gqgRm7NcY&body=%7B%22userid%22%3A2017050501%7D&f=json"));
			//String openIdObj = HttpClientUtil.post(HttpConfig.custom().url("https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=PmwK894P67RbNkk21t8zLLk9c3q1N8xbRkhW3Em6-Hqe2kMdHUyoyGNhla_CUbkf&f=json&tid=73").paramap(convertOpenIdMap()));
			//System.out.println(openIdObj);
			/*String[] arr = openIdObj.split(",");
			for(int i =0 ; i<arr.length ; i++) {
				System.out.println(arr[i]);
			}*/
			//WeixinDTO data = JSON.parseObject(openIdObj, WeixinDTO.class);
			//System.out.println(data.toString());
			/*if(openIdObj.indexOf("openid") > 0) {
				String tt = openIdObj.substring(openIdObj.indexOf("openid"));
				System.out.println(tt.substring(11, tt.indexOf("\\\"}")));
				//String str = openIdObj.substring(openIdObj.indexOf("openid"), endIndex)
			}*/
		
	} 
	 
	public static void main(String[] args) throws Exception{
		BigDecimal amt = new BigDecimal("0");
		BigDecimal amt2 = new BigDecimal("0");
		System.out.println(amt.compareTo(amt2));
	//	sendMessage();
		
	}
	
	public static void sendMessage() throws Exception {
		String body = "{\"agentid\":\""+ "13" +"\", \"touser\": \"2017050501\" ,"+
				" \"msgtype\":\"text\", "+
				 "\"text\": {"+" \"content\":\""+ "你有需要维修的设备,请注意查看."  +"\" }, "+
				 "\"safe\":0"+
				 "}";
		System.out.println(body);
		
		String token ="YOAK9bbUSW9XtGExtpRGoVH3DNkN7_Xo-Tg5oC7jWx6Ue9M7dQlyq_ycmztso_HWOvPVp0XQkX72-P9ZzloUh4lQo3EGy59dGwkU_5nnrwAILEXMoEOhMX4YD4CutjZl4hzhIYjkFdbgl5kuyRBvGmC-Ml1-qbXZdwW0ehVa16vyrSO8Pvq5a1d0PiQ4d26haOOt3owwELUy0mDw5MoiOvDqIa6XM7A-0EU4YOeJ2XOOSUXaoUEsYoBFfHTXqtm5wl9VhFDEzdee3PuVRC-B9yuXARBCs9iiwLC5U89rGT8";
		String openIdObj = HttpClientUtil.post(HttpConfig.custom().url("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token+"&body=" + URLEncoder.encode(body) +"&f=json&tid=21"));
	}
	
	private  static Map<String, Object> convertOpenIdMap() {
		// 获取openID
				String body = "{\"userid\":2017050501}";
		Map<String, Object> data = new HashMap<String, Object>();
//		data.put("access_token", "PmwK894P67RbNkk21t8zLLk9c3q1N8xbRkhW3Em6-Hqe2kMdHUyoyGNhla_CUbkf");
		data.put("body", body);//f=json
//		data.put("f", "json");
//		data.put("tid", 73);
		return data;
	};
}
